<?php
/**
 * 网春确认发布
 */
//require_once dirname(dirname(__DIR__))."/common.php";
require_once dirname(__DIR__) . "/login_filter.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\tom\Config;
use app\cls\tom\FFmpeg;
use app\model\Activity;

$host_id = $_POST['host_id'];//主持人id
$select_person_id = $_POST['select_person_id'];//拜年对象id
$video_title = $_POST['video_title'];//拜年对象id
if (empty($host_id) || empty($select_person_id) || empty($video_title)) {
    Response::output(-1, "缺少参数");
}
$redis = SysResource::getRedis();
$redisKey = REDIS_PREFIX."wangchun_one_".$host_id."_".$select_person_id;

$datas = $redis->get($redisKey);
if(!empty($datas)){
    $datas = json_decode($datas,true);
}else{
    $Activity = new Activity(SysResource::getDB(false));
    $datas = $Activity->wangchun_one(['wangchun_host' => $host_id, "wangchun_select_person" => $select_person_id], "id,wangchun_host,wangchun_select_person,wangchun_host_name,wangchun_select_person_name,wangchun_video_url,wangchun_pic,wangchun_big_pic,wangchun_share_pic,wangchun_title,wangchun_duration,wangchun_height,wangchun_width,wangchun_huati,wangchun_fenlei,first_pic");
    if(!empty($datas)){
        $redis->set($redisKey,json_encode($datas),50);
    }
}

if (empty($datas)) {
    Response::output(-1, "视频不存在");
}

//生成video_uuid
$k1 = mt_rand(100000, 999999);
$k2 = mt_rand(100000, 999999);
$k3 = mt_rand(1000000, 9999999);
$k4 = mt_rand(1000, 9999);
$k5 = $k1 . $k2 . $k3 . $k4;
//拼接数据如入数据库
$Activity = new Activity(SysResource::getDB(true));
$data['material_id'] = "网春";
$data['uuid'] = $Auth['uuid'];
$data['video_uuid'] = $k5;
$data['nick_name'] = $Auth['nick_name'];
$data['nick_pic'] = $Auth['nick_pic'];
$data['pic'] = $datas['wangchun_pic'];
$data['share_pic'] = $datas['wangchun_share_pic'];
$data['hengping_pic'] = $datas['wangchun_big_pic'];
$data['video_desc'] = $video_title;
$data['width2'] = $datas['wangchun_width'];
$data['height2'] = $datas['wangchun_height'];
$data['duration'] = $datas['wangchun_duration'];
$data['join_type'] = $datas['wangchun_fenlei'];
$data['join_type_sub'] = $datas['wangchun_huati'];
$data['video_url'] = $datas['wangchun_video_url'];
$data['examine'] = 3;
$data['type'] = 2;
$data['is_dub'] = 1;
$data['is_save'] = 1;
$data['mix_status'] = 1;
$data['is_job_status'] = 2;
$data['is_wangchun'] = 1;//1为网春（前端上传），2不是网春，3为网春（后台上传）
$data['add_time'] = date("Y-m-d H:i:s", time());
 $data['first_pic3'] = $datas['first_pic'];
$data['user_score'] = 1;
//Response::output(0,"发布成功",$data);
//添加入库
$res = $Activity->add_material($data);
if ($res) {
    $d['id']=$res;
    $d['video_uuid']=$k5;
    $d['wangchun_share_pic']=$datas['wangchun_share_pic'];
    $d['nick_pic']=$data['nick_pic'];
    $d['nick_name']=$data['nick_name'];
    $d['video_title']=$video_title;
    Response::output(0, "发布成功",$d);
} else {
    Response::output(-1, "发布失败");
}

function upload_dir($type = "image", $suffix = "jpg")
{
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1 = date("Y", time());
    $date2 = date("m", time());
    $date3 = date("d", time());
    $dir = "/" . $date1 . "/" . $date2 . "/" . $date3;
    $uploadpath1 = $uploadConfig['savepath'];
    $uploadpath2 = $uploadConfig['savehttp'];
    $key1 = rand(1000, 10000);
    $key2 = rand(10000, 20000);
    $key3 = rand();
    $file_name = $key1 . $key2 . $key3;
    //这是服务器上的上传地址
    $uploadpath['savepath'] = $uploadpath1 . $dir . "/" . $file_name . ".{$suffix}";
    if ($type != "image") {
        mkdir($uploadpath1 . $dir, 0777, true);
    }
    //这是网络访问地址
    $uploadpath['savehttp'] = $uploadpath2 . $dir . "/" . $file_name . ".{$suffix}";
    return $uploadpath;
}
